/// **************************************************************************************************
/// "Copyright (c) 2007 Sai Panyam, Inc.  All rights reserved."
/// **************************************************************************************************
/// Component Name   :	LinkModifier, Member of System72.NTextile 
/// Description      :	LinkModifier description
/// Author           :	Sai Panyam
/// Creation Date    :	November 09 2007
/// Modified Date    :              
/// Modified By      :
/// **************************************************************************************************
using System;
using System.Text.RegularExpressions;
using System72.NTextile.RegexLibrary;

namespace System72.NTextile
{
    internal class LinkModifier : IModifier
    {
        #region Private Variables

        private readonly Operation m_operation = new Operation();
        private AttributeCollection m_supportedAttributes;
        private PunctuationCollection m_supportedPunctuations;
        private bool m_success;
        private readonly int m_parseOrder=4;
        private readonly ModifierType m_type;
        
        #endregion

        #region Constructor
        /// <summary>
        /// 
        /// </summary>
        public LinkModifier()
        {
            m_type = ModifierType.Link;
        }

        #endregion

        #region Public Properties
        /// <summary>
        /// 
        /// </summary>
        public AttributeCollection SupportedAttributes
        {
            get
            {
                return m_supportedAttributes;
            }
            set
            {
                m_supportedAttributes = value;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public PunctuationCollection SupportedPunctuations
        {
            get { return m_supportedPunctuations; }
            set { m_supportedPunctuations = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public bool Success
        {
            get { return m_success; }
            set { m_success = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int ParseOrder
        {
            get { return m_parseOrder; }
        }
        /// <summary>
        /// 
        /// </summary>
        public ModifierType Type
        {
            get { return m_type; }
        }

        #endregion
        
        #region Public Methods
        /// <summary>
        /// 
        /// </summary>
        /// <param name="contentText"></param>
        /// <param name="excludedModifiers"></param>
        /// <returns></returns>
        public string GetXHTML(string contentText, ModifierCollection excludedModifiers)
        {
            if (m_operation != null)
            {
                if (!excludedModifiers.ContainsModifier(this))
                {
                    LinkRegex linkRegex = new LinkRegex();
                    MatchCollection matchCollection = linkRegex.GetMatches(contentText);
                    foreach (Match match in matchCollection)
                    {
                        //Since there is atleast one match then we can set Success to true
                        m_success = true;
                        string matchText;
                        string linkText = match.Groups[1].Value;
                        string href = match.Groups[2].Value;
                    
                        string titleText = String.Empty;
                        ParenthesisRegex parenthesisRegex = new ParenthesisRegex();
                        MatchCollection parenthesisMatches = parenthesisRegex.GetMatches(linkText);
                        if(parenthesisMatches!=null && parenthesisMatches.Count>0)
                        {
                            titleText = parenthesisMatches[0].Groups[0].Value;
                            linkText = linkText.Replace(titleText, String.Empty);//Remove the Title attribute from link text
                            titleText = titleText.Replace(titleText.Substring(0,1), String.Empty);//Remove starting parenthesis
                            titleText = titleText.Replace(titleText.Substring(titleText.Length-1,1), String.Empty);//Remove ending parenthesis
                        }
                        string attributeText = String.Format(" href={0}{1}{0} title={0}{2}{0}", Util.DoubleQuote, href,titleText);
                        matchText = Util.GetElementMarkUp(linkText,"a",attributeText);
                        //Do the HTML only if any content is present between the operators
                        if (match.Value.Length > 0)
                        {
                            contentText = contentText.Replace(match.Value, matchText);
                        }
                        //No attributes are processed as Link Modifier doesn't support one.
                    }
                }
            }
            return contentText;
        }

        #endregion

    }
}
